package com.bootdo.cms.controller;

import com.bootdo.cms.domain.ChannelDO;
import com.bootdo.cms.domain.MemberDO;
import com.bootdo.cms.service.ChannelService;
import com.bootdo.cms.service.MemberService;
import com.bootdo.cms.vo.MemberVO;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.vo.UserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author KonBAI
 * @version 1.0
 * Create by 2019/12/18 03:14
 */
@Controller
@RequestMapping("/cms/member")
public class MemberController {

	String prefix = "cms/member";

	@Autowired
	private MemberService memberService;

	@Autowired
	private ChannelService channelService;
	
	@GetMapping("")
	@RequiresPermissions("cms:member:member")
	String Member(Model model){
		List<Tree<ChannelDO>> channels = channelService.listChannelTree();
		// List<VideoDO> videoDOs = videoService.list(map);
		model.addAttribute("channels", channels);
	    return prefix + "/member";
	}

	/**
	 * 查询所有会员
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("cms:member:member")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MemberDO> memberList = memberService.list(query);
		int total = memberService.count(query);
		PageUtils pageUtils = new PageUtils(memberList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("cms:member:add")
	String add(){
	    return prefix + "/add";
	}

	/**
	 * 打开编辑页面
	 * @param memberId
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{memberId}")
	@RequiresPermissions("cms:member:edit")
	String edit(@PathVariable("memberId") Long memberId,Model model){
		MemberDO member = memberService.get(memberId);
		model.addAttribute("member", member);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("cms:member:add")
	public R save( MemberDO member){
		if(memberService.save(member)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("cms:member:edit")
	public R update(MemberDO member){
		if (memberService.update(member) > 0) {
			return R.ok("修改成功！");
		}
		return R.error();
	}

	@ResponseBody
	@RequestMapping("/selfUpdate")
	public R selfUpdate(MemberDO member){
		if (ShiroUtils.getMember().getMemberId().equals(member.getMemberId())) {
			if (memberService.update(member) > 0) {
				return R.ok("修改成功！");
			} else {
				return R.error();
			}
		} else {
			return R.error("不是用户本人不允许修改");
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("cms:member:remove")
	public R remove( Long memberId){
		if(memberService.remove(memberId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("cms:member:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] memberIds){
		memberService.batchRemove(memberIds);
		return R.ok();
	}

	@RequiresPermissions("cms:member:resetPwd")
	@Log("请求更改用户密码")
	@GetMapping("/resetPwd/{id}")
	String resetPwd(@PathVariable("id") Long memberId, Model model) {
		MemberDO memberDO = new MemberDO();
		memberDO.setMemberId(memberId);
		model.addAttribute("member", memberDO);
		return prefix + "/reset_pwd";
	}

	@Log("提交更改用户密码")
	@RequiresPermissions("cms:member:resetPwd")
	@PostMapping("/adminResetPwd")
	@ResponseBody
	R adminResetPwd(MemberVO memberVO) {
		try{
			memberService.resetPwd(memberVO);
			return R.ok();
		}catch (Exception e){
			return R.error(1,e.getMessage());
		}

	}
	
}
